<template>
  <div id="container">
    <el-card shadow="hover">
      <!-- 表单 -->
      <label for="" class="loginTitle"> <h1>就爱商城登录页面</h1> </label>
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
        label-width="100px"
        class="demo-loginForm"
        :hide-required-asterisk="true"
      >
        <el-form-item label="用户名:" prop="userName">
          <el-input
            v-model="loginForm.userName"
            type="username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="submitLoginFormButton"
            type="primary"
            @click="submitLoginForm('loginForm')"
            v-loading.fullscreen.lock="fullscreenLoading"
            >登录</el-button
          >
          <el-button
            class="buttwo"
            type="primary"
            @click="goregister"
            style="margin-left: 50px"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { setToken } from "../../utils/auth";
import { loginToken } from "../../api/users";
export default {
  data() {
    return {
      // 登录按钮加载
      fullscreenLoading: false,
      // 登录框
      loginForm: {
        userName: "",
        password: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 注册按钮
    goregister() {
      this.$router.replace("/register");
    },
    // 登录按钮
    submitLoginForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var userName = this.loginForm.userName;
          var password = this.loginForm.password;
          var data = { userName, password };
          console.log(data);
          // // 登录加载中
          // this.fullscreenLoading = true;
          loginToken(data)
            .then((data) => {
              if (data.code === 1000) {
                // if (data.data.jurisdiction === 1) {
                  // 普通用户登录成功
                  // 登录成功关闭加载
                  this.fullscreenLoading = false;
                  // 储存Token的封装
                  setToken(
                    data.data.token,
                    data.data.refreshToken,
                    this.loginForm.userName
                  );
                  this.$router.push("/");
                // } else if (data.data.jurisdiction === 2) {
                //   // 管理员登录成功
                //   // 登录成功关闭加载
                //   this.fullscreenLoading = false;
                //   // 储存Token的封装
                //   setToken(
                //     data.data.token,
                //     data.data.refreshToken,
                //     this.loginForm.userName
                //   );
                //   this.$router.push("/admin");
                // } else if (data.data.jurisdiction === 3) {
                //   //供应商登录成功
                //   //关闭登录加载
                //   this.fullscreenLoading = false;
                //   // 储存Token的封装
                //   setToken(
                //     data.data.token,
                //     data.data.refreshToken,
                //     this.loginForm.userName
                //   );
                //   //管理员可以跳转去后台界面
                //   this.$router.push("/?");
                // }
              } else {
                // 密码不正确关闭加载
                this.fullscreenLoading = false;
                //   用户或密码不正确

                this.$message({
                  type: "error",
                  message: data.msg,
                });
              }
            })
            .catch((err) => {
              // 登录失败
              console.log(err);
            });
        } else {
          this.$message({
            type: "error",
            message: "登录验证失败，拒绝提交",
          });
          // console.log("登录验证失败，拒绝提交");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
#container {
  background-size: cover;
  background-repeat: no-repeat;
  height: 100vh;
  justify-content: center;
  display: flex;
  align-items: center;
}
.el-card {
  min-width: 30rem;
  min-height: 25rem;
  border-radius: 7px;
}
.submitLoginFormButton {
  margin-top: 1rem;
  margin-left: 10rem;
}
.demo-loginForm {
  margin-right: 2rem;
}
.loginTitle {
  margin-top: 2rem;
}
</style>


